package com.xyz.basesdk.helpers;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;

import com.xyz.basesdk.R;
import com.xyz.basesdk.base.BaseApplication;
import com.xyz.basesdk.fields.PermissionField;
import com.xyz.basesdk.utils.MPLog;

import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;


/**
 * Created by mac
 * on 2017/10/17.
 *
 * @author mac
 */

public class PermissionsHelper {

    private Resources resources = BaseApplication.getContext().getResources();
    private Context mContext = BaseApplication.getContext();
    private Activity activity;

    public PermissionsHelper(Activity activity) {
        this.activity = activity;
    }

    private boolean hasPermission(String... perms) {
        return EasyPermissions.hasPermissions(mContext, perms);
    }

    public boolean requestLocation() {
        if (hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)) {
            return true;
        } else {
            try {
                EasyPermissions.requestPermissions(activity, resources.getString(R.string.permissions_location),
                        PermissionField.LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION);

            } catch (Exception e) {
                MPLog.e("permission=====", e.getMessage(), e);
                settingDialog();
            }

        }
        return false;
    }


    public boolean requestCamera() {
        if (hasPermission(Manifest.permission.CAMERA)) {
            return true;
        } else {
            try {
                EasyPermissions.requestPermissions(activity, resources.getString(R.string.permissions_camera),
                        PermissionField.CAMERA, Manifest.permission.CAMERA);
            } catch (Exception e) {
                MPLog.e("permission=====", e.getMessage(), e);
                settingDialog();
            }

        }
        return false;
    }

    public boolean requestSdCard() {
        if (hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            return true;
        } else {
            try {
                EasyPermissions.requestPermissions(activity, resources.getString(R.string.permissions_sd),
                        PermissionField.SD_CARD, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            } catch (Exception e) {
                MPLog.e("permission=====", e.getMessage(), e);
                settingDialog();
            }

        }
        return false;
    }

    public boolean requestPhone() {
        if (hasPermission(Manifest.permission.READ_PHONE_STATE)) {
            return true;
        } else {
            try {
                EasyPermissions.requestPermissions(activity, resources.getString(R.string.permissions_phone),
                        PermissionField.PHONE, Manifest.permission.READ_PHONE_STATE);
            } catch (Exception e) {
                MPLog.e("permission=====", e.getMessage(), e);
                settingDialog();
            }

        }
        return false;
    }

    public boolean somePermissionPermanentlyDenied(List<String> perms) {
        if (EasyPermissions.somePermissionPermanentlyDenied(activity, perms)) {
            settingDialog();
            return true;
        }
        return false;
    }

    private void settingDialog() {
        new AppSettingsDialog.Builder(activity)
                .setTitle(resources.getString(R.string.permissions_require_title))
                .setRationale(resources.getString(R.string.permissions_require_desc))
                .build()
                .show();
    }
}
